This notebook shows how to donwload all blockgroup data for a set of variable You can change to download different geographies*
It uses three external libraries:
pip install census
but may with other geographies.
In [1]:
# may need to point this to your census module
from us_census_api.core import Census
# Download from pypi
from us import states
from pandas import DataFrame
import pandas as pd
In [2]:
census_API_key = 'API_KEY_HERE'
c = Census(census_API_key, year=2010)
In [4]:
# Test basic API call
data = DataFrame(c.acs5.state_county_blockgroup(('NAME', 'GEOID'), 36, '5', Census.ALL))
data.head()
Out[4]:
In [12]:
# variable list:
var_edu = {
'GEOID':'GeoId',
'B15002_001E': 'Edu Total',
'B15002_002E': 'Edu Male Total',
}
for i in range(3,19):
key = 'B15002_{}E'.format(str(i).zfill(3))
var_edu[key] = 'Edu Male Level {}'.format(i-3)
for i in range(20,36):
key = 'B15002_{}E'.format(str(i).zfill(3))
var_edu[key] = 'Edu Female Level {}'.format(i-20)
var_edu_tup = tuple(key for key, val in var_edu.items())
In [13]:
var_edu
Out[13]:
In [14]:
# Test API call with variables
data = DataFrame(c.acs5.state_county_blockgroup(var_edu_tup, 36, '5', Census.ALL))
data.head()
Out[14]:
In [18]:
bkgps = {}
In [19]:
# THIS DOWNLOADS ALL THE DATA!
# It can take awhile.
# If it breaks:
# 1. check what state was the last state you got. assume you only got part of that state
# 2. delete that state in bkgps dict
# 3. change states.STATES to states.STATES[5:] (or wherever it broke)
# 4. repeat
for state in states.STATES:
print(state)
counties = c.acs5.state_county('NAME', state.fips, Census.ALL)
data = []
for county in counties:
county_num = county['county']
data.extend(c.acs5.state_county_blockgroup(var_edu_tup, state.fips, county_num, Census.ALL))
bkgps[state] = data
Out[19]:
In [ ]:
bkgpsDF = []
for key, val in bkgps.items():
bkgpsDF.extend(val)
bkgpDF = DataFrame(bkgpsDF)
bkgpDF.head()
In [20]:
bkgpDF.to_csv('/Users/joe/Dropbox/SFI_CensusData/UnitedStates/2010acs_edu.csv', index=False)
In [ ]: